package com.dq.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dq.commonutils.dto.OrderCourseVo;
import com.dq.commonutils.dto.OrderUserVo;
import com.dq.commonutils.pojo.DqException;
import com.dq.commonutils.utils.UUIDUtil;
import com.dq.order.client.CourseClient;
import com.dq.order.client.UcenterClient;
import com.dq.order.entity.Order;
import com.dq.order.service.OrderService;
import com.dq.order.mapper.OrderMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 订单(TOrder)表服务实现类
 *
 * @author Dq
 * @since 2022-08-17 15:16:00
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>  implements OrderService {

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public String createOrder(String courseId, String userId) {
        /**
         * 1. 远程调用课程Client 获取课程信息
         * 2. 远程调用用户Client 获取用户信息
         * 3. 获取具体信息
         * 4. 如果没有开启熔断降级 则正式开始发送请求
         */
        //开启远程调用 如果出错则抛出异常
        OrderCourseVo orderCourseInfo = courseClient.getOrderCourseInfo(courseId);
        OrderUserVo orderUserInfo = ucenterClient.getOrderUserInfo(userId);
        if (orderUserInfo ==null || orderCourseInfo ==null){
           throw new DqException(20001,"熔断降级操作开启 请检查错误");
        }
        Order order = new Order();
        String uuid = UUIDUtil.getUUID();
        order.setOrderNo(uuid);
        order.setCourseId(courseId);
        order.setCourseTitle(orderCourseInfo.getTitle());
        order.setCourseCover(orderCourseInfo.getCover());
        order.setTeacherName(orderCourseInfo.getTeacherName());
        order.setTotalFee(orderCourseInfo.getPrice());
        order.setMemberId(userId);
        order.setMobile(orderUserInfo.getMobile());
        order.setNickname(orderUserInfo.getNickname());

        order.setStatus(0); //支付状态 0 未支付 1支付
        order.setPayType(1);//支付类型 1 微信  2支付宝

        orderMapper.insert(order);
        return order.getOrderNo();
    }

    @Override
    public Order getOrderInfoById(String orderId) {
        /**
         * 1. 获取id  然后查询唯一的订单号
         * 2. 返回这个orderInfo
         */
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 自定义查询colunme
        orderLambdaQueryWrapper.eq(Order::getOrderNo,orderId).select(
                Order::getId,
                Order::getOrderNo,
                Order::getCourseId,
                Order::getCourseTitle,
                Order::getCourseCover,
                Order::getTeacherName,
                Order::getMemberId,
                Order::getNickname,
                Order::getMobile,
                Order::getTotalFee,
                Order::getPayType,
                Order::getStatus);
        Order order = orderMapper.selectOne(orderLambdaQueryWrapper);
        if (order==null){
            throw new DqException(20001,"该订单存在异常风险: 可能为传入的orderNo错误");
        }
        return order;
    }
}